//
//  CommentCell_Image.m
//  zhijian
//
//  Created by lcb on 2019/4/1.
//  Copyright © 2019 lcb. All rights reserved.
//

#import "CommentCell_Image.h"
#import "BaseImageView.h"
#import "CommentModel.h"
#import <YBImageBrowser/YBImageBrowser.h>
@interface CommentCell_Image ()<YBImageBrowserDelegate,YBImageBrowserDataSource>
@property (strong, nonatomic) UIView *picsView;
@end
@implementation CommentCell_Image
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _picsView = [[UIView alloc] initWithFrame:CGRectZero];
        [self.contentView addSubview:_picsView];
        // 设置约束
        CGFloat margin = 10;
        
        self.userView.sd_layout
        .leftSpaceToView(self.contentView,margin*2)
        .rightSpaceToView(self.contentView,margin)
        .topSpaceToView(self.contentView,0)
        .heightIs(50);
        //
        self.lblTitle.sd_layout
        .topSpaceToView(self.userView,margin)
        .leftSpaceToView(self.contentView,margin*9)
        .rightSpaceToView(self.contentView,margin)
        .autoHeightRatio(0);
        [self.lblTitle setMaximumLineHeight:50];
        //
        _picsView.sd_layout
        .topSpaceToView(self.lblTitle,margin)
        .leftSpaceToView(self.contentView,margin*9)
        .rightSpaceToView(self.contentView,margin)
        .heightIs((SCREEN_WIDTH-40)/3);
        //
        [self setupAutoHeightWithBottomView:_picsView bottomMargin:10];
    }
    return self;
}
-(void)dealloc
{
    self.lblTitle=nil;
    self.userView=nil;
}
-(void)setBaseModel:(BaseModel *)baseModel
{
    CommentModel*questionModel = (CommentModel*)baseModel;
    self.userView.baseModel = baseModel;
    self.lblTitle.contentString = questionModel.commentContent;
    NSArray *array = [questionModel.commentPics componentsSeparatedByString:@","];
    for (int i = 0; i < array.count; i++) {
        // 宽度(自定义)
        CGFloat PIC_WIDTH =(SCREEN_WIDTH-10*7)/3;
        // 高度(自定义)
        CGFloat PIC_HEIGHT =(SCREEN_WIDTH-10*7)/3;
        //创建图片
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.tag = i;
        // 允许用户交互
        imageView.userInteractionEnabled = YES;
        [imageView imageWithUrlStr:[array objectAtIndex:i] placeholder:[UIImage imageNamed:@"插画"]];
        NSInteger row = i / 3;
        NSInteger col = i % 3;
        CGFloat margin = (SCREEN_WIDTH - (PIC_WIDTH * 3)) / (3 + 1);
        // PointX
        CGFloat picX = (PIC_WIDTH + margin) * col;
        imageView.frame = CGRectMake(picX, 0, PIC_WIDTH, PIC_HEIGHT);
        [self.picsView addSubview:imageView];
        // 给头像添加轻拍手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doTap:)];
        [imageView addGestureRecognizer:tap];
    }
}
-(void)doTap:(UIGestureRecognizer *)sender{
    // 设置数据源代理并展示
    YBImageBrowser *browser = [YBImageBrowser new];
    browser.dataSource = self;
    browser.delegate=self;
    browser.currentIndex =sender.view.tag;
    [browser show];
}


// 实现 <YBImageBrowserDataSource> 协议方法配置数据源
- (NSUInteger)yb_numberOfCellForImageBrowserView:(YBImageBrowserView *)imageBrowserView {
    CommentModel*questionModel = (CommentModel*)self.userView.baseModel;
    NSArray *array = [questionModel.commentPics componentsSeparatedByString:@","];
    
    return [array count];
}
- (id<YBImageBrowserCellDataProtocol>)yb_imageBrowserView:(YBImageBrowserView *)imageBrowserView dataForCellAtIndex:(NSUInteger)index {
    CommentModel*questionModel = (CommentModel*)self.userView.baseModel;
    NSArray *array = [questionModel.commentPics componentsSeparatedByString:@","];
    YBImageBrowseCellData *data = [YBImageBrowseCellData new];
    data.url = [array objectAtIndex:index];
    //    data.sourceObject = ...;
    return data;
}
@end
